//! 测试模块
//! 测试 unified_api 框架的核心功能

#[cfg(test)]
mod tests {
    use crate::auth::{AuthPolicy, AuthContext, AuthRequest, NoAuthPolicy};
    use crate::routers::RouteInfo;

    #[test]
    fn test_route_info_creation() {
        let route = RouteInfo::new("/api/users", "GET", "get_users");
        
        assert_eq!(route.path, "/api/users");
        assert_eq!(route.method, "GET");
        assert_eq!(route.handler_name, "get_users");
        assert!(route.auth_policy.is_none());
        assert!(route.description.is_none());
        assert!(route.tags.is_empty());
    }

    #[test]
    fn test_route_info_with_builder() {
        let route = RouteInfo::new("/api/users", "GET", "get_users")
            .with_auth_policy("permission:users.read")
            .with_description("获取用户列表")
            .with_tags(vec!["users", "admin"]);
        
        assert_eq!(route.path, "/api/users");
        assert_eq!(route.method, "GET");
        assert_eq!(route.handler_name, "get_users");
        assert_eq!(route.auth_policy, Some("permission:users.read".to_string()));
        assert_eq!(route.description, Some("获取用户列表".to_string()));
        assert_eq!(route.tags, vec!["users".to_string(), "admin".to_string()]);
    }

    #[test]
    fn test_auth_context() {
        let mut context = AuthContext::new("user123".to_string());
        context.username = Some("test_user".to_string());
        context.roles = vec!["user".to_string(), "admin".to_string()];
        context.permissions = vec!["users.read".to_string(), "users.write".to_string()];
        
        assert_eq!(context.user_id, "user123");
        assert_eq!(context.username, Some("test_user".to_string()));
        assert!(context.has_role("user"));
        assert!(context.has_role("admin"));
        assert!(context.has_permission("users.read"));
        assert!(context.has_permission("users.write"));
        assert!(!context.has_permission("users.delete"));
    }

    #[test]
    fn test_no_auth_policy() {
        let auth_policy = NoAuthPolicy;
        let request = AuthRequest::new();
        
        // 测试认证
        let result = auth_policy.authenticate(&request);
        assert!(result.is_ok());
        let context = result.unwrap();
        assert_eq!(context.user_id, "anonymous");
        
        // 测试权限检查
        assert!(auth_policy.check_permission(&context, "").unwrap());
        assert!(auth_policy.check_permission(&context, "any:policy").unwrap());
    }

    #[test]
    fn test_auth_request() {
        let mut request = AuthRequest::new();
        
        // 测试 headers
        request.headers.insert("authorization".to_string(), "Bearer token123".to_string());
        request.headers.insert("content-type".to_string(), "application/json".to_string());
        
        assert_eq!(request.get_authorization_header(), Some(&"Bearer token123".to_string()));
        assert_eq!(request.get_bearer_token(), Some("token123".to_string()));
        
        // 测试 query 参数
        request.query_params.insert("page".to_string(), "1".to_string());
        request.query_params.insert("limit".to_string(), "10".to_string());
        
        assert_eq!(request.query_params.get("page"), Some(&"1".to_string()));
        assert_eq!(request.query_params.get("limit"), Some(&"10".to_string()));
        
        // 测试路径参数
        request.path_params.insert("id".to_string(), "123".to_string());
        assert_eq!(request.path_params.get("id"), Some(&"123".to_string()));
    }

    #[test]
    fn test_it_works() {
        let result = 2 + 2;
        assert_eq!(result, 4);
    }
}